<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of home
 *
 * @author peerawatj20
 */
class home extends MY_Controller {

    private $autopart_code = '';
    private $autopart_name = '';
    private $autopart_main_type = '';
    private $autopart_sub_type = '';
    private $autopart_brand = '';

    public function __construct() {
        parent::__construct();
        $this->load->model('stock_model');
        $this->load->model('catalog');
    }

    public function index() {

        $this->session->unset_userdata('search_code');
        $this->session->unset_userdata('search_name');
        $this->session->unset_userdata('search_main_type');
        $this->session->unset_userdata('search_sub_type');
        $this->session->unset_userdata('search_brand');

        $config['base_url'] = base_url() . "/stock/home/search";
        $config['total_rows'] = $this->stock_model->count();
        $config['per_page'] = 10;
        $config['uri_segment'] = 4;
        $utility = new Config_form();
        $config = $utility->form_pagination($config);
        $this->pagination->initialize($config);

        $result = $this->stock_model->search_list_stock();
        $this->data['result_list'] = $result;
        $this->data['num_list'] = $this->uri->segment(4);

        $this->_set_form();
        $this->load->view('stock/index', $this->data);
    }

    private function _set_form() {
        $this->data['txt_autopart_code'] = form_input('txtAutoCode', $this->autopart_code, 'class="form-control" placeholder="รหัสอะไหล่"');
        $this->data['txt_autopart_name'] = form_input('txtAutoName', $this->autopart_name, 'class="form-control" placeholder="ชื่ออะไหล่"');

        $this->data['cmb_main_type'] = form_dropdown('cmbMainType', $this->catalog->list_stock_main_type(), $this->autopart_main_type, 'class="form-control" id="cmbMainType"');
        $this->data['cmb_sub_type'] = form_dropdown('cmbSubType', $this->catalog->list_stock_sub_type($this->autopart_main_type), $this->autopart_sub_type, 'class="form-control" id="cmbSubType"');
        $this->data['cmb_brand'] = form_dropdown('cmbBrand', $this->catalog->list_stock_brand($this->autopart_sub_type), $this->autopart_brand, 'class="form-control" id="cmbBrand"');
    }

    public function search() {
        //Clean Session for searching and it come from search btn.
        if ($this->input->post('action') == 'search') {
            $this->session->unset_userdata('search_code');
            $this->session->unset_userdata('search_name');
            $this->session->unset_userdata('search_main_type');
            $this->session->unset_userdata('search_sub_type');
            $this->session->unset_userdata('search_brand');

            $this->autopart_code = $this->input->post('txtAutoCode');
            $this->autopart_name = $this->input->post('txtAutoName');
            $this->autopart_main_type = $this->input->post('cmbMainType');
            $this->autopart_sub_type = $this->input->post('cmbSubType');
            $this->autopart_brand = $this->input->post('cmbBrand');

            $this->session->set_userdata('search_code', $this->autopart_code);
            $this->session->set_userdata('search_name', $this->autopart_name);
            $this->session->set_userdata('search_main_type', $this->autopart_main_type);
            $this->session->set_userdata('search_sub_type', $this->autopart_sub_type);
            $this->session->set_userdata('search_brand', $this->autopart_brand);
        } else {
            //set value for pagination.
            $this->autopart_code = $this->session->userdata('search_code');
            $this->autopart_name = $this->session->userdata('search_name');
            $this->autopart_main_type = $this->session->userdata('search_main_type');
            $this->autopart_sub_type = $this->session->userdata('search_sub_type');
            $this->autopart_brand = $this->session->userdata('search_brand');
        }


        $config['base_url'] = base_url() . "/stock/home/search";
        $config['total_rows'] = $this->stock_model->count($this->autopart_code, $this->autopart_name, $this->autopart_main_type, $this->autopart_sub_type, $this->autopart_brand);
        $config['per_page'] = 10;
        $config['uri_segment'] = 4;
        $utility = new Config_form();
        $config = $utility->form_pagination($config);
        $this->pagination->initialize($config);

        $result = $this->stock_model->search_list_stock($this->autopart_code, $this->autopart_name, $this->autopart_main_type, $this->autopart_sub_type, $this->autopart_brand, $config['per_page'], $this->uri->segment(4));
        $this->data['result_list'] = $result;
        $this->data['num_list'] = $this->uri->segment(4);

        $this->_set_form();
        $this->load->view('stock/index', $this->data);
    }

}
